<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 16:54
 */

namespace app\service;


use app\common\model\Coach;
use app\common\model\MoneyLog;
use app\common\model\Student;
use app\common\model\StudentDelay;
use app\common\model\StudentHabit;
use app\common\model\StudentKaoqin;
use app\common\model\StudentLog;
use think\Db;
use think\Exception;

use GuzzleHttp\Client;

/**
 * Class StudentService
 * @package app\service
 */
class StudentService extends BaseService
{
    public $studentModel;
    public $studentLogModel;
    public $studentHabitModel;

    public function __construct()
    {
        $this->studentModel = new Student();
        $this->studentHabitModel = new StudentHabit();
        $this->studentLogModel = new StudentLog();
    }

    /**
     * @param $where
     * @param $page
     * @param $size
     * @param string $filed
     * @param string $order
     * @return array|bool
     */
    public function queryPage($where,$page,$size,$filed='*',$order='ord desc,create_time desc')
    {
        try {
            $info = $this->studentModel->where($where)->page($page,$size)->order($order)->select()->toArray();
            foreach ($info as $key => $value){
                $list = $this->studentHabitModel->where('sid',$value['id'])->order('time desc')->select()->toArray();

                $list_1 = [];
                $list_2 = [];
                $list_3 = [];
                $list_4 = [];
                $list_5 = [];
                if($list){
                    foreach ($list as $val){
                        switch ($val['type']){
                            case 1:
                                $list_1[] = $val;
                                break;
                            case 2:
                                $list_2[] = $val;
                                break;
                            case 3:
                                $list_3[] = $val;
                                break;
                            case 4:
                                $list_4[] = $val;
                                break;
                            case 5:
                                $list_5[] = $val;
                                break;
                        }
                    }
                }
                $info[$key]['list_1'] = $list_1;
                $info[$key]['list_2'] = $list_2;
                $info[$key]['list_3'] = $list_3;
                $info[$key]['list_4'] = $list_4;
                $info[$key]['list_5'] = $list_5;
                $info[$key]['list_log'] = $this->studentLogModel->where('sid',$value['id'])->order('time desc')->select()->toArray();
                $info[$key]['images'] = json_decode($value['images'],true);
                $info[$key]['pay_time'] = date('Y-m-d',$value['pay_time']);

                $info[$key]['lose_time'] = date('Y-m-d',$value['lose_time']);

                $info[$key]['tuijian_lst'] = $value['tuijian']?explode(',',$value['tuijian']):[];
                $info[$key]['delay'] = StudentDelay::getAll(['create_time'=>['>',$value['pay_time']],'sid'=>$value['id']],'create_time desc');

                $info[$key]['keep'] = StudentKaoqin::getAll(['create_time'=>['>',$value['pay_time']?$value['pay_time']:0],'sid'=>$value['id']],'create_time desc','content,thumb,create_time');

//                $info[$key]['keep'] = MoneyLog::getAll(['type'=>2,'sid'=>$value['id'],'is_has'=>0],'create_time desc','content,thumb,create_time');
            }
            return $info;
        } catch (Exception $ex) {
            LogService::S()->writeLog('Exception', $ex->getMessage());
            return false;
        }
    }

    /**
     * 编辑
     * @param $save
     * @param array $where
     * @return bool|false|int|string
     */
    public function querySave($save,$where=[])
    {
//        try {
            if($where){
                $res = $this->studentModel->save($save,$where);
            }else{
                $res = $this->studentModel->insertGetId($save);
            }
            return $res;
//        } catch (Exception $ex) {
//            LogService::S()->writeLog('Exception', $ex->getMessage());
//            return false;
//        }
    }

    /**
     * 查询数量
     * @param $where
     * @return bool|int|string
     */
    public function queryCount($where)
    {
        try {
            return $this->studentModel->where($where)->count();
        } catch (Exception $ex) {
            LogService::S()->writeLog('Exception', $ex->getMessage());
            return false;
        }
    }
}